import { http } from "../utils/http";
import { BASE_URL } from "../utils/http/core";
import { storageSession } from "../utils/storage";
import { ReturnDTO } from "./type";

export interface AwardInsertDTO {
  content: string;
  files: object[];
  images: object[];
  getTime: Date;
  title: string;
  type: string;
}

export interface AwardSelectDTO extends AwardUpdateDTO {
  staffId: number;
  staffName: string;
}

export interface AwardUpdateDTO extends AwardInsertDTO {
  id: number;
}

export interface AwardPage {
  hasNext: boolean;
  hasPre: boolean;
  awardList: AwardSelectDTO[];
}

export interface AwardAnalysisNameDTO {
  name: string;
  num: number;
  staffId: string;
  staffNumber: string;
}

export interface AwardAnalysisTypeDTO {
  type: string;
  num: number;
}

export interface AwardAnalysisNamePageDTO {
  hasNext: boolean;
  hasPre: boolean;
  list: AwardAnalysisNameDTO[];
}

const prefix = "/award";

// 获取汇总的分析结果
export const getAnalysis = (type: string): Promise<ReturnDTO> => {
  return http.request<ReturnDTO>("get", prefix + "/analysis/" + type);
};

// 添加
export const addAward = (data: AwardInsertDTO): Promise<ReturnDTO> => {
  return http.request<ReturnDTO>("post", prefix, data as Object);
};

// 更新
export const updateAward = (data: AwardInsertDTO): Promise<ReturnDTO> => {
  return http.request<ReturnDTO>("put", prefix, data as Object);
};

// 根据成果id查询成果具体信息
export const selectAwardById = (id: number): Promise<ReturnDTO> => {
  return http.request("get", prefix + "/" + id);
};

// 通过标题查询
export const selectAwardByTitle = (
  title: string,
  current: number,
  size: number
): Promise<ReturnDTO> => {
  return http.request<ReturnDTO>(
    "get",
    prefix + "/title/" + title + "/" + current + "/" + size
  );
};

// 分页查询
export const pageAward = (
  current: number,
  size: number
): Promise<ReturnDTO> => {
  return http.request<ReturnDTO>("get", prefix + "/" + current + "/" + size);
};

// 删除
export const deleteAward = (id: number): Promise<ReturnDTO> => {
  return http.request("delete", prefix + "/" + id);
};

// 通过限定人员名称查询统计信息
export const analysisByName = (
  name: string,
  current: number,
  size: number
): Promise<ReturnDTO> => {
  return http.request(
    "get",
    prefix + "/analysis/name" + "/" + name + "/" + current + "/" + size
  );
};

// 获取限定时间范围内的数据
export const analysisGroupByStaffNameLimitByDate = (
  type: string,
  startTime: Date,
  endTime: Date
): Promise<ReturnDTO> => {
  return http.request(
    "get",
    prefix + "/analysis/" + type + "/" + startTime + "/" + endTime
  );
};

// 限定人员名称和时间范围查询统计信息
export const analysisByNameLimitByDate = (
  name: string,
  current: number,
  size: number,
  startTime: Date,
  endTime: Date
): Promise<ReturnDTO> => {
  return http.request(
    "get",
    prefix +
      "/analysis/name" +
      "/" +
      name +
      "/" +
      current +
      "/" +
      size +
      "/" +
      startTime +
      "/" +
      endTime
  );
};

// 根据时间范围和用户id获取用户奖项信息列表
export const getStaffAwardsLimitByDate = (
  staffId: number,
  startTime: Date,
  endime: Date
): Promise<ReturnDTO> => {
  return http.request(
    "get",
    prefix + "/id/" + staffId + "/date/" + startTime + "/" + endime
  );
};

// 根据用户id获取用户奖项信息列表
export const getStaffAwards = (staffId: number): Promise<ReturnDTO> => {
  return http.request("get", prefix + "/id/" + staffId);
};

// 根据时间范围和用户id分页获取用户奖项信息列表
export const getStaffAwardsLimitByDatePage = (
  staffId: number,
  startTime: Date,
  endime: Date,
  current: number,
  size: number
): Promise<ReturnDTO> => {
  return http.request(
    "get",
    prefix +
      "/id/" +
      staffId +
      "/date/" +
      startTime +
      "/" +
      endime +
      "/" +
      current +
      "/" +
      size
  );
};

// 根据时间范围和用户id获取用户奖项分析信息
export const getStaffAwardAnalysisLimitByDate = (
  staffId: number,
  startTime: Date,
  endime: Date
): Promise<ReturnDTO> => {
  return http.request(
    "get",
    prefix + "/analysis/id/" + staffId + "/date/" + startTime + "/" + endime
  );
};

// 根据用户id获取用户奖项分析信息
export const getStaffAwardAnalysis = (staffId: number): Promise<ReturnDTO> => {
  return http.request("get", prefix + "/analysis/id/" + staffId);
};

// 通过fetch的方法进行添加
export const addAwardByFetch = async (
  data: AwardInsertDTO
): Promise<ReturnDTO> => {
  const formData = new FormData();
  data.images.forEach(item => {
    formData.append("images", item["raw"]);
  });
  data.files.forEach(item => {
    formData.append("files", item["raw"]);
  });
  formData.append("title", data.title);
  formData.append("content", data.content);
  formData.append("type", data.type);
  formData.append("getTime", data.getTime.toDateString());
  const resp = await fetch(BASE_URL + prefix + "/add", {
    method: "post",
    headers: {
      token: storageSession.getItem("info")["accessToken"]
    },
    body: formData
  });
  const res = await resp.json();
  return res;
};
